<template>
  <!-- 对话框 -->
  <el-dialog :visible="showDialog" title="添加部门" @close="handleClose">
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="form.name" placeholder="2-10个字符" size="mini" style="width: 80%" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="form.code" placeholder="2-10个字符" size="mini" style="width: 80%" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="managerId">
        <!-- <el-input placeholder="2-10个字符" size="mini" style="width: 80%" /> -->
        <el-select v-model="form.managerId" placeholder="请选择负责人" size="mini" style="width: 80%">
          <el-option v-for="item in userList" :key="item.id" :value="item.id" :label="item.username" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="form.introduce" type="textarea" :rows="4" placeholder="1-100个字符" size="mini" style="width: 80%" />
      </el-form-item>

      <el-row type="flex" justify="center">
        <el-button size="mini" type="primary" @click="submit">确定</el-button>
        <el-button size="mini" @click="handleClose">取消</el-button>
      </el-row>
    </el-form>
  </el-dialog>

</template>

<script>
import { getDepartmentList, getUserList, addDepart } from '@/api/department.js'
export default {
  props: {
    showDialog: Boolean,
    currentId: {
      type: Number,
      required: true
    }
  },
  data() {
    return {
      form: {
        code: '',
        name: '',
        introduce: '',
        managerId: ''
      },
      rules: {
        code: [
          { required: true, message: '这是必填的', trigger: 'blur' },
          { min: 2, max: 10, message: '必须是2-10位', trigger: 'blur' },
          {
            validator: async(rules, value, callback) => {
              const res = await getDepartmentList()
              res.some(item => item.code === value) ? callback('重复命名') : callback()
            },
            trigger: 'blur'
          }
        ],
        name: [
          // 不能和已经存在的部门重复命名
          { required: true, message: '这是必填的', trigger: 'blur' },
          { min: 2, max: 10, message: '必须是2-10位', trigger: 'blur' },
          {
            validator: async(rules, value, callback) => {
              const res = await getDepartmentList() // 部门列表(列表数据 =》适合判断重复)
              // 数组.some =>只要有一个满足条件就返回true
              res.some(item => item.name === value) ? callback('重复命名') : callback()
            },
            trigger: 'blur'
          }
        ],
        introduce: [
          { required: true, message: '这是必填的', trigger: 'blur' },
          { min: 1, max: 100, message: '必须是1-100位', trigger: 'blur' }
        ],
        managerId: [
          { required: true, message: '这是必填的', trigger: 'blur' }
        ]
      },
      userList: []// 获取部门负责人
    }
  },
  async created() {
    const res = await getUserList()
    this.userList = res
  },
  methods: {
    handleClose() {
      this.$refs.form.resetFields()
      this.$emit('update:showDialog', false)
    },
    submit() {
      this.$refs.form.validate().then(async() => {
        await addDepart({
          ...this.form,
          pid: this.currentId
        })
        this.$message.success('添加成功') // 提示
        this.handleClose() // 关闭且清空对话框
        // this.$emit('refresh') // 告诉父组件重新渲染页面
        // $parent => 父组件
        this.$parent.getDepartmentList()
      })
    }
  }
}
</script>

<style>

</style>
